package com.itheima.tcp_demo.test2;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ServerThread implements Runnable {
    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            System.out.println("收到客户端的连接...");
            // 获取网络中的字节输入流 , 封装成高效的输入流
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());

            // 创建本地一个高效的字节输出流
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\134期图片\\" + UUID.randomUUID() + ".jpg"));
            // 接收客户端发过来的一个字节 , 就往本地中写一个字节
            int by;
            while ((by = bis.read()) != -1) {
                bos.write(by);
                bos.flush();
            }
            // 释放资源
            bos.close();

            // ==============================================================================================

            // 获取网络中的字节输出流 , 在封装成高效的字符输出流
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("很好看,谢谢你!");
            bw.newLine();
            bw.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
